package com.ilovepostcard.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ilovepostcard.EditProfileActivity;
import com.ilovepostcard.MyActivity;
import com.ilovepostcard.R;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.utils.DrawableManager;
import com.ilovepostcard.utils.StringUtils;
import com.ilovepostcard.utils.Utils;
import com.mobclick.android.MobclickAgent;

public class MyProfileView extends LinearLayout {

    ImageView user_head_imageview;
    ImageView user_sex_mark;
    TextView user_nickname;
    TextView user_location;
    TextView user_active_status;
    TextView user_intro;
    
    private int pubCount;
    
    public MyProfileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate();
    }

    public MyProfileView(Context context) {
        super(context);
        inflate();
    }
    
    public void setUser(User user) {
        user_nickname.setText(user.Nickname);
        user_location.setText(StringUtils.isEmpty(user.City) ? "我在地球" : "我在"+user.City);
        pubCount = user.PublishCount;
        setupPubCountView();
        if (user.HasHeadImg)
            DrawableManager.getInstance().fetchDrawableOnThread(user.HeadImgUrl, user_head_imageview, false);
        user_sex_mark.setImageResource(user.Sex == 2 ? R.drawable.female : R.drawable.male);
        String intro = user.Introduction;
        if (StringUtils.isBlank(intro)) {
            intro = Utils.randomIntroduction(getContext());
        }
        user_intro.setText(intro);
    }
    
    public void onDeletePubSuccess() {
    	pubCount--;
    	if (pubCount < 0)
    		pubCount = 0;
    	setupPubCountView();
    }
    
    private void setupPubCountView() {
    	user_active_status.setText("创作#"+pubCount+"#张明信片");
    }

    private void inflate() {
        View view = View.inflate(getContext(), R.layout.my_view, this);
        view.findViewById(R.id.edit_button).setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
            	MobclickAgent.onEvent(MyProfileView.this.getContext(), Constants.COUNT_MY_EDITPROFILE);
                Intent intent = new Intent(getContext(), EditProfileActivity.class);
                ((Activity)getContext()).startActivityForResult(intent, MyActivity.REQUEST_CODE_EDIT_PROFILE);
            }
        });
        user_head_imageview = (ImageView) view.findViewById(R.id.user_head_imageview);
        user_sex_mark = (ImageView) view.findViewById(R.id.user_sex_mark);
        user_nickname = (TextView) view.findViewById(R.id.user_nickname);
        user_location = (TextView) view.findViewById(R.id.user_location);
        user_active_status = (TextView) view.findViewById(R.id.user_active_status);
        user_intro = (TextView) view.findViewById(R.id.user_intro);
    }
}
